<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Gfan
 *
 * @author add bydragon.hu@2016.2.1
 */
require_once 'BaseChannel.php';

//gfan
class Gfan implements BaseChannel {
	
	function __construct() {
		//echo "construct";
	}

	//put your code here
	function android($data) {
		$paymentKey="28709909";
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) {
		$paymentKey="28709909";
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{	
		$orderLog = new ChargeOrder();
		$orderLog->log("gfan", $_REQUEST);
		
		/*********************************gfan提供的接口开始****************************************/
		//3.1.3 订单主动通知接口
		
		$time=$_REQUEST['time'];
		
		$sign=$_REQUEST['sign'];
		
		$result="<response><ErrorCode>0</ErrorCode><ErrorDesc>Fail</ErrorDesc></response>";
		
		$userId=$paymentKey;//请配置用户ID
		
		//正确接收到相应的数据
		if (! empty ( $time ) && ! empty ( $sign )) {
			//
			$encodeString = md5 ( $userId . $time );
			// 通过签名
			if ($sign == $encodeString) {
				
				$status = STATUS_SUCCESS;//sign验证通过后就算成功
				
				// 获取post过来的数据
				$response = file_get_contents ( "php://input" );
				
				// 创建解析器
				$parser = xml_parser_create ();
				// 解析到数组
				xml_parse_into_struct ( $parser, $response, $values, $index );
				// 释放解析器
				xml_parser_free ( $parser );
				//
				$order_id = $values [1] ['value'];
				//
				$appkey = $values [2] ['value'];
				//
				$cost = $values [3] ['value'];
				//
				$create_time = $values [4] ['value'];
				
				if (! empty ( $order_id ) && ! empty ( $appkey ) && ! empty ( $cost ) && ! empty ( $create_time )) 
				{
					// 处理你的业务
					if(true == $orderLog->recordOrder($status, $cost/10, $order_id, "gfan", $platform))
					{
						$orderLog->log('gfan','platform:'.$platform.' cost:'.$cost.' order_id:'.$order_id.' create_time:'.$create_time.' success');
						$result = '<response><ErrorCode>1</ErrorCode><ErrorDesc>Success</ErrorDesc></response>';
					}
					else
					{
						$orderLog->log('gfan','platform:'.$platform.' cost:'.$cost.' order_id:'.$order_id.' create_time:'.$create_time.' fail');
						$result = '<response><ErrorCode>0</ErrorCode><ErrorDesc>Fail</ErrorDesc></response>';
					}
				}
				else
				{
					$orderLog->log('gfan',"empty value -- order_id=$order_id appkey=$appkey cost=$cost create_time=$create_time");
					$result = '<response><ErrorCode>0</ErrorCode><ErrorDesc>Fail</ErrorDesc></response>';
				}
			}
			else
			{
				//sign验证失败的情况
				$orderLog->log('gfan','platform:'.$platform.' cost:'.$cost.' order_id:'.$order_id.' create_time:'.$create_time.' sign fail');
				$result = '<response><ErrorCode>0</ErrorCode><ErrorDesc>Fail</ErrorDesc></response>';
			}
		}
		//返回信息给机锋服务器
		echo $result;
		/************************************gfan提供的接口结束*************************************/
	}	
}